Ext.define('Hardware.controller.Application', {
    extend: 'Ext.app.Controller',

    config: {
        refs: {
            main: 'mainview',
            editButton: '#editButton',
            materials: 'materials',
            showMaterial: 'material-show',
            editMaterial: 'material-edit',
            saveButton: '#saveButton'
        },

        control: {
            main: {
                push: 'onMainPush',
                pop: 'onMainPop'
            },
            editButton: {
                tap: 'onMaterialEdit'
            },
            materials: {
                itemtap: 'onMaterialSelect'
            },
            saveButton: {
                tap: 'onMaterialSave'
            },
            editMaterial: {
                change: 'onMaterialChange'
            }
        }
    },

    onMainPush: function(view, item) {
        var editButton = this.getEditButton();

        if (item.xtype == "material-show") {
            this.getMaterials().deselectAll();

            this.showEditButton();
        } else {
            this.hideEditButton();
        }
    },

    onMainPop: function(view, item) {
        if (item.xtype == "material-edit") {
            this.showEditButton();
        } else {
            this.hideEditButton();
        }
    },

    onMaterialSelect: function(list, index, node, record) {
        var editButton = this.getEditButton();

        if (!this.showMaterial) {
            this.showMaterial = Ext.create('Hardware.view.material.Show');
        }

        // Bind the record onto the show contact view
        this.showMaterial.setRecord(record);

        // Push the show contact view into the navigation view
        this.getMain().push(this.showMaterial);
    },

    onMaterialEdit: function() {
        if (!this.editMaterial) {
            this.editMaterial = Ext.create('Hardware.view.material.Edit');
        }

        // Bind the record onto the edit contact view
        this.editMaterial.setRecord(this.getShowMaterial().getRecord());

        this.getMain().push(this.editMaterial);
    },

    onMaterialChange: function() {
        this.showSaveButton();
    },

    onMaterialSave: function() {
        var record = this.getEditMaterial().saveRecord();

        this.getShowMaterial().updateRecord(record);

        this.getMain().pop();
    },

    showEditButton: function() {
        var editButton = this.getEditButton();

        if (!editButton.isHidden()) {
            return;
        }

        this.hideSaveButton();

        editButton.show();
    },

    hideEditButton: function() {
        var editButton = this.getEditButton();

        if (editButton.isHidden()) {
            return;
        }

        editButton.hide();
    },

    showSaveButton: function() {
        var saveButton = this.getSaveButton();

        if (!saveButton.isHidden()) {
            return;
        }

        saveButton.show();
    },

    hideSaveButton: function() {
        var saveButton = this.getSaveButton();

        if (saveButton.isHidden()) {
            return;
        }

        saveButton.hide();
    }
});
